<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/5/12
 * Time: 21:04
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\Search;
use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Manage\LogsAdminModel;
use App\Models\Shop\AgentSalesModel;
use App\Models\Shop\ShopGoodsTrusteeshipModel;
use App\Models\Shop\UserCouponModel;
use DC\UserCenter\Models\UserBaseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsTrusteeshipController extends BackendController
{

    /**
     * @title 代售记录
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function agentRecord(Request $request)
    {
        $search = Search::make($request)->bindModel(AgentSalesModel::with(['user'])->has('user'))
            ->input('user','编号/姓名/手机号','',['field'=>'user_id'])
            ->select('type','代售类型',format_option([-100=>'全部',1=>'消费积分代售',2=>'财富金豆代售']),'-100',['field'=>'agent_type'])
            ->select('status','代售状态',format_option([-100=>'全部',1=>'未结算',2=>'已结算']),'-100')
            ->time('started_at','下单时间')->bindSearchData()
            ->relationModel(['user'=>function($value){
                return UserBaseModel::where('user_number',$value)
                    ->orWhere('real_name',$value)->orWhere('user_phone',$value)->pluck('member_id')->toArray();
            }]);

        $list = $search->where()
            ->orderByRaw('case when `status`<0 then 100 else 1 end asc,status asc ')
            ->paginate($this->getPerPageNum());

        return view('backend::shop.goods.special_needs.agent',[
            'page_title'=>'代售记录',
            'list'=>$list,
            'search'=>$search
        ]);

    }
    /**
     * @title 代售回款结算
     * @param Request $request
     * @return SelfResponse
     */
    public function salesRefund(Request $request)
    {
        $ids = $request->input('tg_id');
        $ids = array_filter(is_array($ids)?$ids:str2arr($ids));
        if(!$ids){
            return SelfResponse::error('请选择要操作的数据');
        }

        foreach ($ids as $id){
            DB::select('call tgReturnById(?)',[$id]);
        }
        return SelfResponse::success('结算执行完成','b_GoodsTrusteeship_agentRecord');
    }

    /**
     * @title 批量结算顺延
     * @param Request $request
     * @return SelfResponse
     */
    public function batchPostpone(Request $request)
    {
        $ids = $request->input('tg_id');
        $ids = array_filter(is_array($ids)?$ids:str2arr($ids));
        if(!$ids){
            return SelfResponse::error('请选择要操作的数据');
        }
        $day = $request->post('day',1);
        if(AgentSalesModel::where('status',1)->whereIn('id',$ids)->increment('cycle_at',abs($day)*86400)){
            return SelfResponse::success('顺延成功','b_GoodsTrusteeship_agentRecord');
        }
        return SelfResponse::error('顺延失败或没有满足添加的记录');
    }

    /**
     * @title 全部结算顺延
     * @param Request $request
     * @return SelfResponse
     */
    public function postpone(Request $request)
    {
        $day = $request->post('day');
        if(AgentSalesModel::where('status',1)->increment('cycle_at',abs($day)*86400)){
            return SelfResponse::success('顺延成功','b_GoodsTrusteeship_agentRecord');
        }
        return SelfResponse::error('顺延失败,请稍后再试');
    }

    /**
     * @title 删除代售记录
     * @param Request $request
     * @return SelfResponse
     */
    public function deleteAgent(Request $request)
    {
        $ids = $request->input('tg_id');
        $ids = array_filter(is_array($ids)?$ids:str2arr($ids));

        if(!$ids){
            return SelfResponse::error('请选择要操作的数据');
        }

        if(AgentSalesModel::whereIn('id',$ids)->delete()){
            return SelfResponse::success('代售记录删除成功','b_GoodsTrusteeship_agentRecord');
        }
        return SelfResponse::error('代售记录删除失败');
    }


}